<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/10
 * Time: 22:22
 */

namespace App\Game\Traits;


use Carbon\Carbon;

trait BgTrait
{
    public function curlLotteryCode()
    {
        $getBalanceParams = array('method' => 'glgl');
        $result = $this->send_require($getBalanceParams);
        $result = json_decode($result, true);
        if ($result['status'] == 0) {
            \Log::info('tcg的游戏列表是',array('data'=>$result['games']));
           // return ['success' => true, 'data' => $result['games'], 'message' => ''];
        } else {
           // return ['success' => false, 'data' => $result['error_desc'], 'message' => ''];
        }
    }

    public function getFirstBatch(Carbon $startTime)
    {
        $batchMinuteArray = ['00', '05', '10', '15', '20' ,'25' ,'30', '35', '40' ,'45','50','55'];
        $startTimeHour = (clone $startTime)->startOfHour();
        $tempMinute = 60;
        $time = null;
        foreach ($batchMinuteArray as $minute) {
            $tempTime = (clone $startTimeHour);
            $tempTime->minute = (integer)$minute;
            $diffMinute = $startTime->diffInMinutes($tempTime, false);

            if ($diffMinute >= 0 and $diffMinute < $tempMinute) {
                $tempMinute = $diffMinute;
                $time = $tempTime;
            }
        }

        if (empty($time)) {
            $time = (clone $startTimeHour)->addHour();
        }

        return $time;
    }

    public function getBatchArray(Carbon $startTime, Carbon $endTime)
    {
        $firstBatchTime = $this->getFirstBatch($startTime);
        if ($firstBatchTime instanceof Carbon) {
            if ($firstBatchTime->diffInSeconds($endTime, false) > 0) {

                do {
                    $batches[] = $firstBatchTime->format('YmdHi');
                    $firstBatchTime->addMinute(5);
                } while ($firstBatchTime->diffInSeconds($endTime, false) >= 0);
                return $batches;
            }
        }

        return [];
    }

    /**
     * 公用发送请求
     * @param $sendParams
     * @return string
     */
    public function send_require($sendParams)
    {
        $params = $this->encryptText(json_encode($sendParams), $this->desKey);
        $sign = hash('sha256', $params . $this->signKey);
        $data = ['merchant_code' => $this->merchantCode, 'params' => $params, 'sign' => $sign];
//        file_put_contents('/home/gameapi/storage/logs/test.log', 'tcg...'.json_encode($data).PHP_EOL, FILE_APPEND);
        $headers = ['Content-type' => 'application/x-www-form-urlencoded'];
        $result = $this->request('POST', $this->gameUrl, $data, $headers);
        return $result;
    }

    /**
     * 组建en json参数数组
     * @param $plainText
     * @param $key
     * @return string
     */
    function encryptText($plainText, $key)
    {
        ;
        $padded = $this->pkcs5_pad($plainText, mcrypt_get_block_size("des", "ecb"));
        $encText = mcrypt_encrypt("des", $key, $padded, "ecb");
        return base64_encode($encText);
    }

    /**
     * 组建dec json参数数组
     * @param $plainText
     * @param $key
     * @return string
     */
    function decryptText($encryptText, $key)
    {
        $cipherText = base64_decode($encryptText);
        $res = mcrypt_decrypt("des", $key, $cipherText, "ecb");
        $resUnpadded = $this->pkcs5_unpad($res);
        return $resUnpadded;
    }

    function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    function pkcs5_unpad($text)
    {
        $pad = ord($text{strlen($text) - 1});
        if ($pad > strlen($text)) return false;
        if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
        return substr($text, 0, -1 * $pad);
    }
}